package dao;

import entity.WarningThreshold;
import util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class WarningThresholdDao {

    // 获取所有预警阈值
    public List<WarningThreshold> getAllThresholds() {
        String sql = "SELECT wt.*, ft.name as fee_name FROM balance_warning_threshold wt " +
                "LEFT JOIN fee_type ft ON wt.fee_type_id = ft.id " +
                "ORDER BY wt.fee_type_id";
        List<WarningThreshold> thresholds = new ArrayList<>();

        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql);
                ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                WarningThreshold threshold = new WarningThreshold();
                threshold.setId(rs.getInt("id"));
                threshold.setFeeTypeId(rs.getInt("fee_type_id"));
                threshold.setFeeName(rs.getString("fee_name"));
                threshold.setThreshold(rs.getBigDecimal("threshold")); // 这里读取 threshold 字段

                thresholds.add(threshold);
            }
        } catch (Exception e) {
            throw new RuntimeException("获取预警阈值列表失败", e);
        }
        return thresholds;
    }

    // 添加预警阈值
    public void addThreshold(WarningThreshold threshold) {
        String sql = "INSERT INTO balance_warning_threshold (fee_type_id, threshold, status) " +
                "VALUES (?, ?, ?)";

        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, threshold.getFeeTypeId());
            stmt.setBigDecimal(2, threshold.getThreshold());
            stmt.setInt(3, threshold.getStatus());

            System.out.println("SQL: " + sql);
            System.out.println("Parameters: " + threshold.getFeeTypeId() + ", " +
                    threshold.getThreshold() + ", " + threshold.getStatus());

            stmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加预警阈值失败", e);
        }
    }

    // 更新预警阈值
    public void updateThreshold(WarningThreshold threshold) {
        String sql = "UPDATE balance_warning_threshold SET threshold = ?, description = ?, " +
                "status = ? WHERE id = ?";

        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setBigDecimal(1, threshold.getThreshold());
            stmt.setString(2, threshold.getDescription());
            stmt.setInt(3, threshold.getStatus());
            stmt.setInt(4, threshold.getId());

            System.out.println("SQL: " + sql);
            System.out.println("Parameters: " + threshold.getThreshold() + ", " +
                    threshold.getDescription() + ", " + threshold.getStatus() +
                    ", " + threshold.getId());

            stmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新预警阈值失败", e);
        }
    }

    // 删除预警阈值
    public void deleteThreshold(int id) {
        String sql = "DELETE FROM balance_warning_threshold WHERE id = ?";

        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);
            stmt.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException("删除预警阈值失败", e);
        }
    }

    // 根据 ID 获取预警阈值
    public WarningThreshold getThresholdById(int id) {
        String sql = "SELECT wt.*, ft.name as fee_type_name FROM balance_warning_threshold wt " +
                "LEFT JOIN fee_type ft ON wt.fee_type_id = ft.id " +
                "WHERE wt.id = ?";

        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                WarningThreshold threshold = new WarningThreshold();
                threshold.setId(rs.getInt("id"));
                threshold.setFeeTypeId(rs.getInt("fee_type_id"));
                threshold.setFeeName(rs.getString("fee_name"));
                threshold.setThreshold(rs.getBigDecimal("threshold")); // 使用 threshold 字段
                threshold.setDescription(rs.getString("description"));
                threshold.setStatus(rs.getInt("status"));
                return threshold;
            }
        } catch (Exception e) {
            throw new RuntimeException("获取预警阈值失败", e);
        }
        return null;
    }
}
